package com.eatrip.presentation.base;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.eatrip.bean.Commento;
import com.eatrip.bean.DatiLocale;
import com.eatrip.bean.Ricetta;

public class BaseTagView extends TagSupport {

	private static final long serialVersionUID = 8294399363399156496L;
	
	private String basePathImage = "../";
	private int dimensioneFoto = 120;
	private Object oggetto = null;
	private int widthImage=100;
	private int heightImage=100;
	private boolean noImage=false;
	
	
	public int doStartTag() throws JspException {
		try {
            JspWriter out = super.pageContext.getOut();
            out.print("");
		}catch (Exception e) {
			throw new JspException("Error: " + e.getMessage());
		}
		return SKIP_BODY;
	}
	
	public int doEndTag() throws JspException {
		try {
			JspWriter out = super.pageContext.getOut();
            out.print("");
		} catch (Exception e) {
			throw new JspException(e.toString());
		}
		return EVAL_PAGE;
	}
	
	public String getBasePathImage() {
		return basePathImage;
	}

	public void setBasePathImage(String basePathImage) {
		this.basePathImage = basePathImage;
	}

	public int getDimensioneFoto() {
		return dimensioneFoto;
	}

	public void setDimensioneFoto(int dimensioneFoto) {
		this.dimensioneFoto = dimensioneFoto;
	}

	public Object getOggetto() {
		return oggetto;
	}

	public void setOggetto(Object oggetto) {
		this.oggetto = oggetto;
	}

	public int getWidthImage() {
		return widthImage;
	}

	public void setWidthImage(int widthImage) {
		this.widthImage = widthImage;
	}

	public int getHeightImage() {
		return heightImage;
	}

	public void setHeightImage(int heightImage) {
		this.heightImage = heightImage;
	}

	public boolean isNoImage() {
		return noImage;
	}

	public void setNoImage(boolean noImage) {
		this.noImage = noImage;
	}

	public int getNumeroElementiInLista(){
		if (this.oggetto instanceof List<?>){
			List<?> tmp = (List<?>) this.oggetto;
			return tmp.size();
		}
		else if (this.oggetto instanceof Collection<?>){
			Collection<?> tmp = (Collection<?>) this.oggetto;
			return tmp.size();
		}
		else if (this.oggetto instanceof ArrayList<?>){
			ArrayList<?> tmp = (ArrayList<?>) this.oggetto;
			return tmp.size();
		}
		else if (this.oggetto instanceof HashMap<?,?>){
			HashMap<?,?> tmp = (HashMap<?,?>) this.oggetto;
			return tmp.size();
		}
		else if (this.oggetto instanceof Ricetta){
			return 1;
		}
		else if (this.oggetto instanceof Commento){
			return 1;
		}
		else if (this.oggetto instanceof DatiLocale){
			return 1;
		}
		return 0;
	}
	
	
	public JspWriter setContenuto(JspWriter out) throws IOException{
		int numeroElementiInLista = this.getNumeroElementiInLista();
		
		if (numeroElementiInLista > 1){
			out.print("");
		}
		else if (numeroElementiInLista > 0){
			out.print("");
		}
		
		return out;
	}
	
}
